package com.aco.party.web.controller.system;

import com.aco.party.enums.MetaTypeEnum;
import com.aco.party.model.po.People;
import com.aco.party.model.vo.PeopleParam;
import com.aco.party.model.vo.PeopleVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.MetaService;
import com.aco.party.service.system.PeopleService;
import com.aco.party.web.command.system.EditPeopleCommand;
import com.aco.party.web.validator.system.EditPeopleValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.aco.party.util.WebModelUtil.*;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManagePeopleController extends BaseController{
    Logger logger = LoggerFactory.getLogger(ManagePeopleController.class);

    @Resource
    private PeopleService peopleService;

    @Resource
    private MetaService metaService;

    private EditPeopleValidator validator = new EditPeopleValidator();

    @RequestMapping(value = {"/people","/people/list"},method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response){
        return "system/managePeoples";
    }

    @RequestMapping(value = "people/list",method = RequestMethod.POST)
    @ResponseBody
    public Page<PeopleVo> getListJson(HttpServletRequest request,DataGridEntry entry){
        Page<PeopleVo> page = new Page<>();
        entry.webParamConvert();
        PeopleParam param = new PeopleParam();
        List<PeopleVo> list = peopleService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),param);
        page.setTotal(peopleService.getTotal(param));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/people/edit",method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response,@ModelAttribute EditPeopleCommand command,Model model){
        setAtt(model);
        if(command.getId() != null){
            People po = peopleService.getByPrimaryKey(command.getId());
            if(po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj",true);
        }
        return "system/editPeople";
    }

    @RequestMapping(value = "/people/edit",method = RequestMethod.POST)
    public String edit(Model model,@ModelAttribute EditPeopleCommand command,BindingResult errors) {
        validator.validate(command, errors);
        if (!errors.hasErrors()) {
            People people = new People();
            command.toPo(people);
            people.setUpdator(getCurrentUser().getId());
            peopleService.update(people);
            model.addAttribute("no_error", true);
        }
        setAtt(model);
        return "system/editPeople";
    }

    @RequestMapping(value = "/people/add",method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditPeopleCommand command,Model model){
        command.setStatus(1);
        setAtt(model);
        return "system/editPeople";
    }

    @RequestMapping(value = "/people/add",method = RequestMethod.POST)
    public String add(Model model,@ModelAttribute EditPeopleCommand command,BindingResult errors,RedirectAttributes attributes){
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editPeople";
        }
        People people = new People();
        command.toPo(people);
        people.setCreator(getCurrentUser().getId());
        peopleService.add(people);
        attributes.addAttribute("id",people.getId());
        attributes.addFlashAttribute("no_error", true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/people/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        peopleService.delete(id);
        return new Response();
    }

    private void setAtt(Model model){
        model.addAttribute("types", convert2Pairs(metaService.getByType(MetaTypeEnum.大咖类型.getValue())));
    }
}
